home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 297_01 / prsun.c < prev    next >
C/C++ Source or Header  |  1991-12-20  |  5KB  |  226 lines

  1. /* sun.c */
  2. /* Machine dependent code that worked on a Sun 3.
  3.  */
  4. /* the input-output below is trivial */
  5.  
  6. #include <stdio.h>
  7. #include <ctype.h>
  8. #include <signal.h>
  9. #include "prtypes.h"
  10.  
  11. #define CANTALLOCATE "cant allocate "
  12. #define NOCONFIGFILE "sprolog.inf missing using default configuration"
  13. #define CONFIG_FILE "sprolog.inf"
  14. #define YESUPPER 'Y'
  15. #define YESLOWER 'y'
  16.  
  17. #if LOGGING_CAPABILITY
  18. extern FILE *Log_file;
  19. #endif
  20.  
  21. extern FILE *Curr_outfile;
  22.  
  23. /**************************** os_alloc() **********************************
  24.     Call the system's allocation function.
  25.  ************************************************************************/
  26. char *os_alloc(how_much)
  27. zone_size_t how_much;
  28. {
  29.     char *ret,*malloc();
  30.     if((ret = malloc(how_much)) == NULL)
  31.     {
  32.         errmsg(CANTALLOCATE);
  33.         exit_term();
  34.         exit(1);
  35.         return(NULL);/* for stupid finicky compilers and lint */
  36.     }
  37.     else
  38.         return(ret);
  39. }
  40.  
  41. static void die()
  42. {
  43. tty_pr_string("You killed me!\n");
  44. exit(1);
  45. }
  46.  
  47. /**************************** ini_term() **********************************
  48.  Initialise terminal 
  49.   This will generally involve things like initialising curses.
  50.  ************************************************************************/
  51.  
  52. void ini_term()
  53. {
  54. extern void stack_dump();
  55.  
  56. signal(SIGINT, stack_dump); /* force a stack dump if user types ^C */
  57. signal(SIGQUIT, die); /* leave if user types ^D */
  58. }
  59.  
  60. /************************** exit_term() *********************************
  61.  Leave terminal and clean up.
  62.  ******************************************************************************/
  63.  
  64. exit_term()
  65. {
  66. }
  67.  
  68. /***************************** errmsg() *********************************
  69.  Output error message.
  70.  ******************************************************************************/
  71.  
  72. errmsg(s)
  73. char *s;
  74. {
  75. #if LOGGING_CAPABILITY
  76.     if(Log_file){
  77.       fprintf(Log_file, "%s", s);
  78.         fflush(Log_file);
  79.         }
  80. #endif
  81.     fprintf(stdout, "%s\n", s);
  82. }
  83.  
  84. /************************** tty_pr_string() *********************************
  85.  Output string to terminal.
  86.  ******************************************************************************/
  87.  
  88. tty_pr_string(s)
  89. char *s;
  90. {
  91. int len;
  92. #if LOGGING_CAPABILITY
  93.     if(Log_file){
  94.       fprintf(Log_file, "%s", s);
  95.         fflush(Log_file);
  96.         }
  97. #endif
  98.     len = printf("%s", s);
  99.     fflush(stdout);
  100.     return(len);
  101. }
  102.  
  103. /*******************************************************************
  104.             pr_string()
  105.  *******************************************************************/
  106.  
  107. pr_string(s)
  108. char *s;
  109. {
  110. int len;
  111. #if LOGGING_CAPABILITY
  112.     extern FILE *Log_file;
  113.     if (Log_file != NULL)
  114.     {
  115.     fprintf(Log_file, "%s", s);
  116.     fflush(Log_file);
  117.     }
  118. #endif
  119.     len = fprintf(Curr_outfile, "%s", s);
  120.     fflush(stdout);
  121. return(len);
  122. }
  123.  
  124. /************************** tty_getc() *********************************
  125.  Read a char from terminal.
  126.  ******************************************************************************/
  127. tty_getc()
  128. {
  129. int c;
  130. c = getchar();
  131. #if LOGGING_CAPABILITY
  132.     if(Log_file){
  133.       fprintf(Log_file, "%c", c);
  134.         }
  135. #endif
  136. return(c);
  137. }
  138.  
  139. /****************************tty_getche()***********************************
  140.  raw read with echo (I have tested this one in the past)
  141.  ***************************************************************************/
  142.  
  143.  
  144.  
  145.  
  146. static struct sgttyb ostate;          /* saved tty state */
  147. static struct sgttyb nstate;          /* values for editor mode */
  148. static struct tchars otchars;  /* Saved terminal special character set */
  149. static struct tchars ntchars = {
  150.     0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  151.  
  152.  
  153. tty_getche()
  154. {
  155.     int c;
  156.  
  157.     gtty(0, &ostate);/* save old state */
  158.     gtty(0, &nstate);/* get base of new state */
  159.     nstate.sg_flags |= RAW;
  160.     nstate.sg_flags &= ~CRMOD;       
  161.     stty(0, &nstate);                       /* set mode */
  162.     ioctl(0, TIOCGETC, &otchars);           /* Save old characters */
  163.     ioctl(0, TIOCSETC, &ntchars);           /* Place new character into K */
  164.  
  165.     c = getchar();
  166.     stty(0, &ostate);
  167.     ioctl(0, TIOCSETC, &otchars);
  168.     return c;
  169. }
  170.  
  171. /**************************** read_config() **********************************
  172.  read the file SPROLOG.INF
  173.  ************************************************************************/
  174.  
  175. int read_config(hs, strs, ds, tp, sbs, tmps, rs, ps)
  176. zone_size_t *hs, *strs, *ds, *tp, *sbs, *tmps;
  177. int *rs, *ps;
  178. {
  179.     FILE *ifp;
  180.  
  181.     if((ifp = fopen(CONFIG_FILE, "r")) == NULL)
  182.     {
  183.         errmsg(NOCONFIGFILE);
  184.         return(0);
  185.     }
  186.  
  187.     fscanf(ifp, "%d %d %d %d %d %d %d %d", hs, strs, ds, tp, sbs, tmps, rs, ps);
  188. #ifdef DEBUG
  189.     printf("%d %d %d %d %d %d %d %d", *hs, *strs, *ds, *tp, *sbs, *tmps, *rs, *ps);
  190.     getchar();
  191. #endif
  192.     return(1);
  193. }
  194.  
  195. /**************************** more_y_n() **********************************
  196.     Ask for confirmation.
  197.  ************************************************************************/
  198. /* a bit crude ... */
  199. int more_y_n()
  200. {
  201.     tty_pr_string("More ?");
  202.     return(read_yes());
  203. }
  204.  
  205. /**************************** read_yes() *********************************
  206.         Return 1 iff user types y
  207. **************************************************************************/
  208. int read_yes()
  209. {
  210. int c;
  211.  
  212. do
  213.  {
  214.  c = tty_getc();
  215.  }while(isspace(c));
  216.  
  217. while(tty_getc() != '\n');/* read rest of line */
  218.  
  219. if(c == YESLOWER || c == YESUPPER )
  220.   return(1);
  221. else
  222.  return(0);
  223. }
  224.  
  225. /* end of file */
  226.